﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using Ue.Web;
using Ue.Extensions;

namespace Ue.Infrastructure.UI.SkinnedControls
{
    public class UrlReferrer : Control
    {
        private readonly string hiddenFieldId = "UrlReferrerHiddenField";
        private string url;

        public UrlReferrer()
        {
            url = HttpContext.Current.Request.GetQueryValue<string>(this.hiddenFieldId, string.Empty, HttpRequestQueryMethod.Form);
            if (string.IsNullOrEmpty(url) && HttpContext.Current.Request.UrlReferrer != null)
            {
                url = HttpContext.Current.Request.UrlReferrer.PathAndQuery;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.Page.ClientScript.RegisterHiddenField(this.hiddenFieldId, url);
        }

        public string Url 
        {
            get { return this.url; }
        }
    }
}
